<template>
  <div>
    <el-form :model="data" :rules="rules" ref="form" label-position="left" label-width="30%" class="ml30">
      <el-row v-if="roles.indexOf('admin') !== -1">
        <el-col :span="12">
          <el-form-item label="省份：" prop="province">
            <el-select class="width80" v-model="data.provinceId" placeholder="请选择省份" filterable>
              <el-option
                v-for="item in province"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="城市：" prop="city">
            <el-select class="width80" v-model="data.cityId" placeholder="请选择城市" filterable>
              <el-option
                v-for="item in city"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="roles.indexOf('admin') !== -1 || roles.indexOf('area') !== -1">
        <el-col :span="12" v-if="roles.indexOf('admin') !== -1">
          <el-form-item label="试验区：" prop="experimentArea">
            <el-select class="width80" v-model="data.experimentAreaId" placeholder="请选择试验区" filterable>
              <el-option
                v-for="item in area"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学校：" prop="school">
            <el-select class="width80" v-model="data.schoolId" placeholder="请选择学校" filterable>
              <el-option
                v-for="item in school"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学籍号：" prop="schoolRollId">
            <el-input class="width80" v-model="data.schoolRollId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名：" prop="name">
            <el-input class="width80" v-model="data.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别：" prop="sex">
            <el-select class="width80" v-model="data.sex" placeholder="性别">
              <el-option label="男" value="男"/>
              <el-option label="女" value="女"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="民族：" prop="nation">
            <el-select class="width80" v-model="data.nation" placeholder="民族">
              <el-option v-for="(value, key) in nation" v-bind:key="key" :label="key" :value="value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出生日期：" prop="birthday">
            <el-date-picker v-model="data.birthday" type="date" class="width80" placeholder="选择出生日期"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号：" prop="identityId">
            <el-input class="width80" v-model="data.identityId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="入学年级：" prop="gradeYear">
            <el-select class="width80" v-model="data.gradeYear" placeholder="入学年级">
              <el-option v-for="item in gradeYear" v-bind:key="item.key" :label="item.value" :value="item.key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年级：" prop="grade">
            <el-select v-if="schoolType==='小学'" class="width80" v-model="data.grade" placeholder="年级">
              <el-option label="一年级" value="11"/>
              <el-option label="二年级" value="12"/>
              <el-option label="三年级" value="13"/>
              <el-option label="四年级" value="14"/>
              <el-option label="五年级" value="15"/>
              <el-option label="六年级" value="16"/>
            </el-select>
            <el-select v-else-if="schoolType==='初中'" class="width80" v-model="data.grade" placeholder="年级">
              <el-option label="初一" value="21"/>
              <el-option label="初二" value="22"/>
              <el-option label="初三" value="23"/>
            </el-select>
            <el-select v-else-if="schoolType==='高中'" class="width80" v-model="data.grade" placeholder="年级">
              <el-option label="高一" value="31"/>
              <el-option label="高二" value="32"/>
              <el-option label="高三" value="33"/>
            </el-select>
            <el-select v-else class="width80" v-model="data.grade" placeholder="年级">
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="班级：" prop="class">
            <el-select class="width80" v-model="data.class" placeholder="班级">
              <el-option v-for="item in classes" v-bind:key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学号：" prop="studentId">
            <el-input class="width80" v-model="data.studentId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="家长电话：" prop="phone">
            <el-input class="width80" v-model="data.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="家庭住址：" prop="address">
            <el-input class="width80" v-model="data.address" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="footer">
      <el-button class="el-button--blue" type="primary" @click="submitStudentAdd">保 存</el-button>
      <el-button @click="msg.visible=false">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { GetProvinceList, GetAreaList, GetCityListByProvinceId, GetAreaListByCityId, GetSchoolListByAreaId, GetSchoolTypeBySchoolId } from '../../../api/select'
import { mapGetters } from 'vuex'
import { nation, awaitCheckSchoolRollId, awaitCheckStudentPhone, SubmitStudentAdd } from '@/api/form'
export default {
  name: 'student-add',
  model: {
    prop: 'msg',
    event: 'cc'
  },
  props: {
    msg: {}
  },
  data () {
    let validateSchoolId = (rule, value, callback) => {
      if ((this.roles.indexOf('admin') !== -1 || this.roles.indexOf('area') !== -1) && this.school.length === 0) {
        callback(new Error('请选择学校'))
      }
      callback()
    }
    let validateSchoolRollId = (rule, value, callback) => {
      if (value.toString() !== '') {
        let res = awaitCheckSchoolRollId(this.data.schoolRollId, 0)
        if (res.status === 200) {
          if (!res.responseJSON.result) {
            callback(new Error(res.responseJSON.msg))
          } else {
            callback()
          }
        }
      }
      callback()
    }
    let validateGrade = (rule, value, callback) => {
      if (this.schoolType === '') {
        callback(new Error('请选择年级'))
      }
      callback()
    }
    let validatePhone = (rule, value, callback) => {
      if (value.toString() !== '') {
        let res = awaitCheckStudentPhone(this.data.phone, 0)
        if (res.status === 200) {
          if (!res.responseJSON.result) {
            callback(new Error(res.responseJSON.msg))
          } else {
            callback()
          }
        }
      }
      callback()
    }
    return {
      rules: {
        school: [
          { validator: validateSchoolId, trigger: 'blur' }
        ],
        schoolRollId: [
          { required: true, message: '学籍号不能为空', trigger: 'change' },
          { min: 1, max: 30, message: '长度在 30 个字符以内', trigger: 'change' },
          { validator: validateSchoolRollId, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'change' },
          { min: 1, max: 10, message: '长度在 10 个字符以内', trigger: 'change' }
        ],
        identityId: [
          { min: 0, max: 18, message: '长度在 18 个字符以内', trigger: 'change' }
        ],
        grade: [
          { validator: validateGrade, trigger: 'blur' }
        ],
        studentId: [
          { required: true, message: '学号不能为空', trigger: 'change' },
          { min: 1, max: 30, message: '长度在 30 个字符以内', trigger: 'change' }
        ],
        phone: [
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'change' },
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        address: [{ min: 0, max: 255, message: '长度在 255 个字符以内', trigger: 'change' }]
      },
      province: [],
      city: [],
      area: [],
      school: [],
      gradeYear: [],
      classes: [],
      schoolType: '',
      data: {
        provinceId: 11,
        cityId: 1101,
        experimentAreaId: 11011,
        schoolId: 0,
        schoolRollId: '',
        name: '',
        sex: '男',
        nation: 1,
        birthday: '',
        identityId: '',
        gradeYear: new Date().getFullYear(),
        grade: '',
        class: 1,
        studentId: '',
        phone: '',
        address: ''
      },
      nation: nation
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  },
  methods: {
    submitStudentAdd () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          SubmitStudentAdd(this.data).then(res => {
            this.msg.visible = false
            this.$store.dispatch('SetFlash')
          })
        } else {
          return false
        }
      })
    }
  },
  mounted () {
    for (let i = 2000; i <= new Date().getFullYear(); i++) {
      this.gradeYear.push({ key: i, value: i + '级' })
    }
    for (let i = 1; i < 100; i++) {
      this.classes.push(i)
    }
    if (this.roles.indexOf('admin') !== -1) {
      GetProvinceList().then(res => {
        this.province = res.data.province
        this.data.provinceId = res.data.province[0].id
      })
    } else if (this.roles.indexOf('area') !== -1) {
      GetAreaList().then(res => {
        this.area = res.data.area
        this.data.areaId = res.data.area[0].id
      })
    }
  },
  watch: {
    'data.provinceId': {
      handler () {
        if (this.roles.indexOf('admin') !== -1) {
          GetCityListByProvinceId(this.data.provinceId).then(res => {
            this.city = res.data.city
            this.data.cityId = res.data.city[0].id
          })
        }
      },
      immediate: true,
      deep: true
    },
    'data.cityId': {
      handler () {
        if (this.roles.indexOf('admin') !== -1) {
          GetAreaListByCityId(this.data.cityId).then(res => {
            this.area = res.data.area
            this.data.areaId = res.data.area[0].id
            this.data.experimentAreaId = res.data.area[0].id
          })
        }
      },
      immediate: true,
      deep: true
    },
    'data.experimentAreaId': {
      handler () {
        if (this.roles.indexOf('admin') !== -1 || this.roles.indexOf('area') !== -1) {
          GetSchoolListByAreaId(this.data.experimentAreaId).then(res => {
            this.school = res.data.school
            this.data.schoolId = res.data.school[0].id
          })
        }
      },
      immediate: true,
      deep: true
    },
    'data.schoolId': {
      handler () {
        if ((this.roles.indexOf('admin') !== -1 || this.roles.indexOf('area') !== -1) && this.data.schoolId !== 0) {
          GetSchoolTypeBySchoolId(this.data.schoolId).then(res => {
            this.schoolType = res.data.schoolType
            console.log(this.schoolType)
            if (this.schoolType === '小学') {
              this.data.grade = '11'
            } else if (this.schoolType === '初中') {
              this.data.grade = '21'
            } else if (this.schoolType === '高中') {
              this.data.grade = '31'
            }
          })
        } else if (this.roles.indexOf('school') !== -1) {
          GetSchoolTypeBySchoolId(0).then(res => {
            this.schoolType = res.data.schoolType
            console.log(this.schoolType)
            if (this.schoolType === '小学') {
              this.data.grade = '11'
            } else if (this.schoolType === '初中') {
              this.data.grade = '21'
            } else if (this.schoolType === '高中') {
              this.data.grade = '31'
            }
          })
        }
      },
      immediate: true,
      deep: true
    }
  }
}
</script>

<style scoped>
  .width80 {
    width: 80%;
  }
  .footer {
    display: flex;
    justify-content: flex-end;
  }
</style>
